package com.w7vm.menuweb.dao;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.w7vm.menuweb.entity.RestauranteFilialWeb;
import com.w7vm.menuweb.entity.RestauranteSedeInfraestruturaWeb;
import com.w7vm.menuweb.entity.RestauranteSedeWeb;

@Stateless
public class RestauranteDAO extends GenericDAO<RestauranteSedeWeb> {

	@PersistenceContext
	private EntityManager em;
	
	
	public RestauranteDAO() {
		super(RestauranteSedeWeb.class);
		// TODO Auto-generated constructor stub
	}

	public RestauranteSedeWeb findRestauranteSedeByID(int idRestaurante){
		System.out.println("[IAM-EJB][findRestauranteSedeByID] :"+ idRestaurante);
		RestauranteSedeWeb objSede = new RestauranteSedeWeb();
		objSede = em.find(RestauranteSedeWeb.class, idRestaurante);
		return objSede;
		
	//	Map<String, Object> parameters = new HashMap<String, Object>();
	//	parameters.put("idRestaurante", idRestaurante);   
	//	return super.findOneResult(RestauranteSedeWeb.FIND_RestauranteSede_BY_ID, parameters);
	}
	
	public RestauranteSedeWeb mergeUpdate(RestauranteSedeWeb sedeEntity){
		return super.update(sedeEntity);
	}
	
	public List<RestauranteSedeWeb> findRestauranteSedeByCNPJ(String cnpj){
		System.out.println("[IAM-EJB][findRestauranteByCNPJ] :"+ cnpj);
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("cnpj", cnpj);
		try{
			return super.findByCriterion(RestauranteSedeWeb.FIND_SEDE_BY_CNPJ, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}

	}

	
	public void deleteInfraestrutura(RestauranteSedeWeb sedeEntity){
		sedeEntity = em.find(RestauranteSedeWeb.class, sedeEntity.getIdRestauranteSedeWeb());
		sedeEntity.setRestauranteSedeInfraestruturaWebs(null);
		em.remove(sedeEntity);
		em.flush();
	}

}
